define(function(require, exports, module) {
	'use strict';
    var rk = require('rk');
    
    
    
    $.widget('rk.themeSetting', $.rk.oaDialogCtrl, {
        oldTheme: null,
        previewTheme: null,
            _create: function(){
                var me = this;
                var elem = me.element; 

                var tpl = require('./themeSetting.tpl');
                elem.html(rk.templateText(tpl));

                elem.find('input[type="radio"]').button().click(function(){
                    var btn = $(this);
                    var newClass = btn.val();
                    $('#container').attr('class', 'wrapper ' + newClass);
                    me.previewTheme = newClass;
                    me._updateLogo(me.previewTheme);            
                });//UI美化

                me.oldTheme = $('#container').attr('class').match(/theme-\w*/g)[0];
                me.previewTheme = me.oldTheme;

                elem.on('click', 'img', function(){
                    var li = $(this).closest('li');
                    var radio = $(li).find('input[type="radio"]');
                    radio.click();
                });

                var menuUnFold = $('#pageleft_unfold').is(':visible');
                var left = menuUnFold ? $('#pageleft_unfold').outerWidth() : $('#pageleft_fold').outerWidth();
                $('.ui-widget-overlay').css('left', left + 'px');

                $(document.body).append('<div id="leftmenucover" style="width:'+left+'px;height: 100%;position:fixed;left:0;top:0;z-index:99999999;"></div>')
            },
            _init: function(){
                var me = this;
                var elem = me.element;

                var themeName = $('#container').attr('class').match(/theme-\w*/g);;
                elem.find('input[type="radio"][value="'+themeName+'"]').click();//.prop('checked', true);

                var dialog = elem.closest('.ui-dialog');
                dialog.find('.ui-dialog-titlebar-close').click(function(){
                    me._rollback();
                });

                me.getBtn('save').on('click', function (){
                    //if(window._hmt){_hmt.push(['_trackEvent', 'operate', 'themeSet', window.SESSION.tenantName]);} //baidu 统计
                    var val = elem.find('input:checked').val();
                    var themeClass = val;

                    var data = {
                                pageId:10,
                                elementId:12,
                                value:themeClass
                            };
                    rk.post("/json/crm_pageSetting/save.action", data).done(function(data) {
                        $('#container').attr('class', 'wrapper ' + themeClass);
                        SESSION.navValue = themeClass;
                        me.closeMe();
                    });
                });
                //取消
                me.getBtn('cancel').on('click', function (){
                    me._rollback();
                    me.closeMe();
                });
            },
            _updateLogo: function(themeName){
                var me = this;
                
                var currentLogo = $('#pageleft').pageLeftCtrl('getLogo');
                if(me._isDefault(currentLogo)){
                    $('#pageleft').pageLeftCtrl('setLogo', me._getDefaultLogo(themeName));
                }       
            },
            _rollback: function(){
                var me = this;
                $('#container').attr('class', 'wrapper ' + me.oldTheme);
                me._updateLogo(me.oldTheme);
            },
            _destroy: function(){
                $('#leftmenucover').remove();
            },
            _getDefaultLogo: function(themeName){
                var me = this;
                return '/static/img/v3.0/' + rk.replaceStr(themeName, '-', '_') + '_logo.png';
            },
            _isDefault: function(url){
                var reg = new RegExp(/[\w\W]*\/theme_([\w]*)_logo.png/g);
                return reg.test(url);
            }
    });
});